package com.blocks.blocks;

import com.blocks.ScriptBlocksApplication;
import com.blocks.gui.BlockListGroup;
import com.blocks.script.ScriptUtils;
import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioNode;
import com.jme3.audio.AudioNode.Status;
import com.jme3.audio.AudioRenderer;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;

public class SoundBlock  extends Block{
	private AudioNode audioA;
	private AudioNode audioB;
	private AudioNode audioC;
	private AudioNode audioD;
	private AudioNode audioE;
	private AudioNode audioF;
	private AudioNode audioG;
	
	private AudioNode lastAudioNode;
	public SoundBlock() {	
		super();
	}
	public SoundBlock(String name, Long masterId) {
		super(name,masterId);
		this.setBlockListGroup(BlockListGroup.GAME);
	}
	
	@Override
	public void init() {
		setTextureRow(0.0f);
		setTextureColumn(5.0f);
		AudioRenderer audioRenderer = ScriptBlocksApplication
		.getInstance()
		.getAudioRenderer();
		AssetManager assetManager = ScriptBlocksApplication
		.getInstance()
		.getAssetManager();
		
		audioA = new AudioNode(audioRenderer, assetManager, "Sounds/Piano_A.wav", false);
		audioB = new AudioNode(audioRenderer, assetManager, "Sounds/Piano_B.wav", false);
		audioC = new AudioNode(audioRenderer, assetManager, "Sounds/Piano_C.wav", false);
		audioD = new AudioNode(audioRenderer, assetManager, "Sounds/Piano_D.wav", false);
		audioE = new AudioNode(audioRenderer, assetManager, "Sounds/Piano_E.wav", false);
		audioF = new AudioNode(audioRenderer, assetManager, "Sounds/Piano_F.wav", false);
		audioG = new AudioNode(audioRenderer, assetManager, "Sounds/Piano_G.wav", false);
		
		audioA.setLooping(false);
		audioB.setLooping(false);
		audioC.setLooping(false);
		audioD.setLooping(false);
		audioE.setLooping(false);
		audioF.setLooping(false);
		audioG.setLooping(false);
		
		audioA.setPositional(true);
		audioA.setDirectional(true);
		audioB.setPositional(true);
		audioC.setPositional(true);
		audioD.setPositional(true);
		audioE.setPositional(true);
		audioF.setPositional(true);
		audioG.setPositional(true);

		
		 super.init();		 
	}
		
	@Override
	public void updateLogicalState(float tpf) {
		
		super.updateLogicalState(tpf);
		if(this.isPowered()){	
			audioA.setLocalTranslation(this.getWorldTranslation());
			audioB.setLocalTranslation(this.getWorldTranslation());
			audioC.setLocalTranslation(this.getWorldTranslation());
			audioD.setLocalTranslation(this.getWorldTranslation());
			audioE.setLocalTranslation(this.getWorldTranslation());
			audioF.setLocalTranslation(this.getWorldTranslation());
			audioG.setLocalTranslation(this.getWorldTranslation());
			audioA.updateGeometricState();
			audioB.updateGeometricState();
			audioC.updateGeometricState();
			audioD.updateGeometricState();
			audioE.updateGeometricState();
			audioF.updateGeometricState();
			audioG.updateGeometricState();
			if(lastAudioNode!=null){
				lastAudioNode.setPitch(1.0f);
			}
			switch (getFrequency(getBlockSides())) {
			
			case 0:
				setColor(ColorRGBA.Red);
				if(lastAudioNode!=null){
					ScriptBlocksApplication
					.getInstance()
					.getAudioRenderer().stopSource(lastAudioNode);
				}
				break;
			case 1:
				update(ColorRGBA.Orange, this.getAudioA(), 1.0f);
				break;
			case 2:
				update(ColorRGBA.Yellow, this.getAudioB(), 1.0f);
				break;
			case 3:
				update(ColorRGBA.Green, this.getAudioC(), 1.0f);
				break;
			
			case 4:
				update(ColorRGBA.Blue, this.getAudioD(), 1.0f);
				break;	
			case 5:
				update(ColorRGBA.Magenta, this.getAudioE(), 1.0f);
				break;	
			case 6:
				update(ColorRGBA.Yellow, this.getAudioF(), 1.0f);
				break;		
			case 7:
				update(ColorRGBA.Yellow, this.getAudioG(), 1.0f);
				break;	
			case 8:
				update(ColorRGBA.Orange, this.getAudioA(), 2.0f);
				break;
			case 9:
				update(ColorRGBA.Yellow, this.getAudioB(), 2.0f);
				break;
			case 10:
				update(ColorRGBA.Green, this.getAudioC(), 2.0f);
				break;
			
			case 11:
				update(ColorRGBA.Blue, this.getAudioD(), 2.0f);
				break;	
			case 12:
				update(ColorRGBA.Magenta, this.getAudioE(), 2.0f);
				break;	
			case 13:
				update(ColorRGBA.Yellow, this.getAudioF(), 2.0f);
				break;		
			case 14:
				update(ColorRGBA.Yellow, this.getAudioG(), 2.0f);
				break;				
			default:
				if(lastAudioNode!=null){
					ScriptBlocksApplication
					.getInstance()
					.getAudioRenderer().stopSource(lastAudioNode);
				}
				setColor(ColorRGBA.White);
				break;
			}
			//ScriptBlocksApplication.getInstance().cameraDirectionText.setText("Sound Frequency" + getFrequency(getBlockSides()));
		} else {
			setColor(ColorRGBA.Gray);
		}
	}

public void update(ColorRGBA color, AudioNode audioNode, float pitch){
	setColor(color);
	if(lastAudioNode!=null && lastAudioNode.getStatus().equals(Status.Playing)){
		return;
		
	}
	lastAudioNode = audioNode;
	lastAudioNode.setPitch(pitch);
	ScriptBlocksApplication
	.getInstance()
	.getAudioRenderer()
	.playSource(lastAudioNode);
}	
	
public AudioNode getAudioA() {
	return audioA;
}
public AudioNode getAudioB() {
	return audioB;
}
public AudioNode getAudioC() {
	return audioC;
}
public AudioNode getAudioD() {
	return audioD;
}
public AudioNode getAudioE() {
	return audioE;
}
public AudioNode getAudioF() {
	return audioF;
}
public AudioNode getAudioG() {
	return audioG;
}
public void setAudioA(AudioNode audioA) {
	this.audioA = audioA;
}
public void setAudioB(AudioNode audioB) {
	this.audioB = audioB;
}
public void setAudioC(AudioNode audioC) {
	this.audioC = audioC;
}
public void setAudioD(AudioNode audioD) {
	this.audioD = audioD;
}
public void setAudioE(AudioNode audioE) {
	this.audioE = audioE;
}
public void setAudioF(AudioNode audioF) {
	this.audioF = audioF;
}
public void setAudioG(AudioNode audioG) {
	this.audioG = audioG;
}

	
		
		
	
}
